package com.beta.cust.bind.datasync.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.beta.cust.bind.datasync.dto.APPMsgReq;
import com.beta.cust.bind.datasync.service.IWechatWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 微信回调接口
 */
@Slf4j
@RestController
@RequestMapping("/api/wx/callback")
public class WxCallBackApi extends WxCallBackSupport {

    @Autowired
    private IWechatWorkService wechatWorkService;

    /**
     * 员工/部门 事件接收服务器
     */
    @GetMapping(value = "/memberDeptChange/{corpId}")
    public void memberChangeCheck(@PathVariable("corpId") String corpId, APPMsgReq req, HttpServletResponse httpServletResponse) {
        //解析请求参数
        String sEchoStr = decryptCallbackInfo(req);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/text");
        PrintWriter out;
        try {
            out = httpServletResponse.getWriter();
            out.println(sEchoStr);
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error("memberChangeCheck error", e);
        }
    }

    /**
     * 员工/部门 变更事件回调
     */
    @PostMapping(value = "/memberDeptChange/{corpId}")
    public void memberDeptChange(@PathVariable("corpId") String corpId, APPMsgReq req, HttpServletRequest request, HttpServletResponse httpServletResponse) {
        JSONObject jo = decryptCallbackInfoJson(req, request);
        log.info("接收到微信回掉请求 方法名：[{}],请求内容：[{}],企业Id：[{}]", "memberChange", jo.toJSONString(), corpId);
        try (PrintWriter out = httpServletResponse.getWriter()) {
            wechatWorkService.callBackEmplChange(jo, corpId);
            httpServletResponse.setStatus(HttpStatus.OK.value());
            out.println(StrUtil.EMPTY);
            out.flush();
        } catch (Exception e) {
            log.info("成员(员工)变更事件回调失败，失败信息是：{}", e);
        }
    }

    /**
     * 客户联系-> 客户的事件接收服务器<br>
     * 详情见：https://work.weixin.qq.com/api/doc/90000/90135/92128<br>
     */
    @GetMapping(value = "/custChange/{corpId}")
    public void custChangeCheck(@PathVariable("corpId") String corpId, APPMsgReq req, HttpServletResponse httpServletResponse) {
        //解析请求参数
        String sEchoStr = decryptCallbackInfo(req);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/text");
        PrintWriter out;
        try {
            out = httpServletResponse.getWriter();
            out.println(sEchoStr);
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error("memberChangeCheck error", e);
        }
    }

    /**
     * 客户联系-> 客户的事件变更回调<br>
     * 即 员工删除客户 和 客户删除员工 和添加客户 的事件回调<br>
     * 详情见：https://work.weixin.qq.com/api/doc/90000/90135/92130#%E5%88%A0%E9%99%A4%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%88%B7%E4%BA%8B%E4%BB%B6<br>
     */
    @PostMapping(value = "/custChange/{enterpriseId}")
    public void custChange(@PathVariable("enterpriseId") String enterpriseId, APPMsgReq req, HttpServletRequest request, HttpServletResponse httpServletResponse) {
        JSONObject jo = decryptCallbackInfoJson(req, request);
        log.info("接收到微信回掉请求 方法名：[{}],请求内容：[{}],企业Id：[{}]", "memberChange", jo.toJSONString(), enterpriseId);
        try (PrintWriter out = httpServletResponse.getWriter()) {
            wechatWorkService.callBackCustChange(jo, enterpriseId);
            httpServletResponse.setStatus(HttpStatus.OK.value());
            out.println(StringUtils.EMPTY);
            out.flush();
        } catch (Exception ex) {
            log.info("删除企业客户、删除跟进成员、添加企业客户事件回调出错了，出错信息是：{}", ex);
        }
    }
}
